<?php

class Weather{
    public $address;
    public $weidu;
    public $jindu;

    public function  __construct($address){
        $this->address = $address;
    }
    public function GetWeather(){
        $this->getLocArray();
        $url = 'http://caiyunapp.com/fcgi-bin/v1/api.py?lonlat='.$this->jindu.','.$this->weidu.'&token=Uettvo-DGkWgOuth';
        $res = $this->call($url);
        return json_decode($res,true);
    }
    public function getLocArray(){
        $res = $this->call($this->getLocate());
        $res_array = json_decode($res,true);
        if($res_array['status'] == 0){
            $this->jindu = $res_array['result']['location']['lng'];
            $this->weidu = $res_array['result']['location']['lat'];
        }

    }


    public function call($url, $time_out = 30 )
    {
        if ( '' == $url )
        {
            return false;
        }

        $url_ary = parse_url( $url );
        if ( !isset( $url_ary['host'] ) )
        {
            return false;
        }

        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_NOPROGRESS, 1 );
        curl_setopt( $ch, CURLOPT_NOBODY, 0 );
        curl_setopt( $ch, CURLOPT_HTTPGET, 1 );
        curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)' );

        $http_header = array();
        $http_header[] = 'Connection: Keep-Alive';
        $http_header[] = 'Pragma: no-cache';
        $http_header[] = 'Cache-Control: no-cache';
        $http_header[] = 'Accept: */*';
        $http_header[] = 'Host: '.$url_ary['host'];
        curl_setopt( $ch, CURLOPT_HTTPHEADER, $http_header );
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_TIMEOUT, $time_out );

        $result = curl_exec( $ch );
        curl_close( $ch );
        return $result;
    }

    /**
     * use baidu map api calculate place location
     * @return string
     */
    public function getLocate(){
        $ak = 'F1977c1b8ef594cb48bc6623a77455d0';
        $sk = '62452213773e70813c75b45128147617';   //应用类型为for server, 请求校验方式为sn校验方式时，
        //系统会自动生成sk，可以在应用配置-设置中选择Security Key显示进行查看（此处sk值仅供验证参考使用）
        $url = "http://api.map.baidu.com/geocoder/v2/?address=%s&output=%s&ak=%s&sn=%s";  //以Geocoding服务为例，地理编码的请求url，参数待填

        //get请求uri前缀
        $uri = '/geocoder/v2/';



        //地理编码的请求output参数
        $output = 'json';

        //构造请求串数组
        $querystring_arrays = array (
            'address' => $this->address, //地理编码的请求中address参数
            'output' => $output,
            'ak' => $ak
        );

        //调用sn计算函数，默认get请求
        $sn = $this->caculateAKSN($ak, $sk, $uri, $querystring_arrays);

        //请求参数中有中文、特殊字符等需要进行urlencode，确保请求串与sn对应
        $target =sprintf($url, urlencode($this->address), $output, $ak, $sn);
        return $target;
    }

    /**
     * 计算签名
     * @param $ak
     * @param $sk
     * @param $url
     * @param $querystring_arrays
     * @param string $method
     * @return string
     *
     */
    private function caculateAKSN($ak, $sk, $url, $querystring_arrays, $method = 'GET')
    {
        if ($method === 'POST'){
            ksort($querystring_arrays);
        }
        $querystring = http_build_query($querystring_arrays);
        return md5(urlencode($url.'?'.$querystring.$sk));
    }

    /**
     *  根据降雨每分钟预报，计算降雨量级
     *  @return array rain level/10minutes
     */
    public static function calc_rain_level($res_weathers){

        $w_array =array(0,0,0,0,0,0);
        foreach ($res_weathers as $key => $value) {
            $w_array[$key/10] +=  abs($value);
        }
        $hour_forecast= array();
        foreach($w_array as $key =>$value){

            if($value <= 0){
                $rain = '无降雨';
            }
            if($value <= 0.5 && $value >0){
                $rain ='毛毛细雨';

            }
            if($value <= 1.5 && $value >0.5){
                $rain ='小雨';

            }
            if($value <= 3.5 && $value >1.5){
                $rain ='中雨';

            }
            if($value >3.5){
                $rain ='大雨';

            }
            $hour_forecast[$key] = $rain;
        }
        return $hour_forecast;
    }


}
